package com.mazaiting.web.utils;

import com.alibaba.fastjson.JSON;
import com.mazaiting.auth.constant.SecurityConst;
import com.mazaiting.auth.domain.PayloadInfo;
import com.mazaiting.common.core.domain.result.ResultCode;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

/**
 * JWT工具类
 */
public class JwtUtil {

    /**
     * 获取授权信息
     * @return 授权信息
     */
    public static String getOauthClient() throws EmptyObjectException {
        String oauthClient = HttpUtil.getHeader(SecurityConst.KEY_OAUTH_CLIENT);
        if (!StringUtils.hasLength(oauthClient) || !oauthClient.startsWith(SecurityConst.BASIC_PREFIX)) {
            throw new EmptyObjectException(ResultCode.OAUTH_CLIENT_NOT_EXIST);
        }
        // 删除前缀
        return oauthClient.replace(SecurityConst.BASIC_PREFIX, "");
    }

    /**
     * 获取授权信息
     * @return 授权信息
     */
    public static String getAuthorization() throws EmptyObjectException {
        return getAuthorization(HttpUtil.getHttpServletRequest());
    }

        /**
         * 获取授权信息
         *
         * @param request 请求
         * @return 授权信息
         */
    public static String getAuthorization(HttpServletRequest request) {
        // 读取 authorization
        String authorization = request.getHeader(SecurityConst.KEY_AUTHORIZATION_NEW);
        if (!StringUtils.hasLength(authorization)) {
            // 读取 Authorization
            authorization = request.getHeader(SecurityConst.KEY_AUTHORIZATION);
        }
        return authorization;
    }

    /**
     * 获取 payload 信息
     *
     * @return payload 信息实体
     */
    public static Optional<PayloadInfo> getPayloadInfo() throws EmptyObjectException {
        // 获取 payload
        String payload = HttpUtil.getHttpServletRequest().getHeader(SecurityConst.KEY_JWT_PAYLOAD);
        if (!StringUtils.hasLength(payload)) {
            throw new EmptyObjectException(ResultCode.ACCESS_UNAUTHORIZED);
        }
        // 解码
        payload = URLDecoder.decode(payload, StandardCharsets.UTF_8);
        return Optional.ofNullable(JSON.parseObject(payload, PayloadInfo.class));
    }

//    /**
//     * 解析JWT获取获取认证身份标识
//     * 1. 先从请求头读
//     * 2. 再从请求体中读取
//     * 2. 再从请求参数中读取
//     *
//     * @return 客户端标识
//     */
//    public static String getAuthenticationIdentity() throws EmptyObjectException {
//        HttpServletRequest request = getHttpServletRequest();
//        // 从请求头读取 refresh_token
//        String refreshToken = request.getHeader(SecurityConst.REFRESH_TOKEN_KEY);
//        // 读取 refreshToken
//        if (!StringUtils.hasLength(refreshToken)) {
//            refreshToken = request.getParameter(SecurityConst.REFRESH_TOKEN_KEY_NEW);
//        }
//        if (!StringUtils.hasLength(refreshToken)) {
//            // 读取 refresh_token
//            refreshToken = request.getParameter(SecurityConst.REFRESH_TOKEN_KEY);
//        }
//        if (!StringUtils.hasLength(refreshToken)) {
//            throw new EmptyObjectException(ResultCode.PARAM_TYPE_ERROR);
//        }
//
//        // 解析认证信息
//        String payload = StrUtil.toString(JWSObject.parse(refreshToken).getPayload());
//        JSONObject jsonObject = JSON.parseObject(payload);
//
//        String authenticationIdentity = jsonObject.getString(SecurityConst.AUTHENTICATION_IDENTITY_KEY);
//        if (StrUtil.isBlank(authenticationIdentity)) {
//            authenticationIdentity = AuthenticationIdentityEnum.USERNAME.getValue();
//        }
//        return authenticationIdentity;
//    }

//    /**
//     * 获取 access_token
//     * 从请求头中取
//     *
//     * @return payload 认证信息
//     */
//    public static JSONObject getAccessToken() throws EmptyObjectException {
//        HttpServletRequest request = getHttpServletRequest();
//        getHttpServletRequest().getPathInfo()
//
//        // 获取 access_token
//        String accessToken = authorization.replace(SecurityConst.JWT_PREFIX, "");
//        // 解析认证信息
//        String payload = StrUtil.toString(JWSObject.parse(accessToken).getPayload());
//        return JSON.parseObject(payload);
//    }
}
